﻿using System;
using System.Collections.Generic;
using Microsoft.SharePoint.Administration;
using ServBus.ListForm2013.License;
using ServBus.ListForm2013.Utility;

namespace ServBus.ListForm2013.AdministrationPages
{
    public partial class DownloadInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["type"] == "lic")
            {
                byte[] buffer = Convert.FromBase64String(Convert.ToString(SPFarm.Local.Properties[Constants.FARM_LICENSE_KEY]));

                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment; filename=ListForm2013.lic");
                Response.OutputStream.Write(buffer, 0, buffer.Length);
                Response.Flush();
                Response.Close();
                Response.End();
            }
            else if (Request.QueryString["type"] == "srv")
            {
                List<Guid> serverIds = new List<Guid>();
                if (!string.IsNullOrEmpty(Request.QueryString["ids"]))
                {
                    string[] ids = Request.QueryString["ids"].Split(';');
                    foreach (string id in ids)
                    {
                        try
                        {
                            if (!string.IsNullOrEmpty(id))
                                serverIds.Add(new Guid(id));
                        }
                        catch
                        {
                        }
                    }
                }

                byte[] buffer = FarmInfo.GetFarmInfo(serverIds);
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment; filename=ListForm2013.svr");
                Response.OutputStream.Write(buffer, 0, buffer.Length);
                Response.Flush();
                Response.Close();
                Response.End();
            }
        }
    }
}
